package com.smyh.common;

import android.util.Log;

import com.smyh.UIM.HostFragment;
import com.smyh.UIM.MainActivity;
import com.smyh.UIM.WifiSetActivity;
import com.smyh.bean.Msg;
import com.smyh.protcol.CMDUtil;
import com.smyh.protcol.ProtcolAnaly;
import com.smyh.protcol.ProtcolStruts;
import com.smyh.protcol.device.DeviceProtcolAnaly;
import com.smyh.protcol.host.ProtcolStrutsAnaly;

import org.apache.mina.core.buffer.IoBuffer;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

import java.util.Date;

public class MinaHandler implements IoHandler {

    /**
     * 当接收到客户端的请求信息后触发此方法
     */
    @Override
    public void messageReceived(final IoSession session, final Object message) throws Exception {
        MainActivity.mainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (message instanceof IoBuffer) {
                    IoBuffer buffer = (IoBuffer) message;
                    String dataStr = buffer.getHexDump();
                    Msg msg1 = new Msg(dataStr, Msg.TYPE_RECTIVED, Msg.sdf.format(new Date()));
                    WifiSetActivity.msgList.add(msg1);
                    WifiSetActivity.refreshWifiSetList();
                    try {
                        if (!HostFragment.connection_btn.isChecked() && !HostFragment.ready_btn.isChecked()) {
                            ProtcolAnaly.analyMsg(session, buffer);
                        } else if (HostFragment.connection_btn.isChecked() && HostFragment.ready_btn.isChecked()) {
                            ProtcolStrutsAnaly.analyMsg(session, buffer);
                        } else if (HostFragment.connection_btn.isChecked() && !HostFragment.ready_btn.isChecked()) {
                            DeviceProtcolAnaly.analyMsg(session, buffer);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();

                    }
                }

            }
        });
    }

    /**
     * 当连接后打开时触发此方法，一般此方法与 sessionCreated 会被同时触发
     */
    @Override
    public void sessionOpened(IoSession session) throws Exception {

//		IoBuffer sendCmd = CMDUtil.askLocalParam(ProtcolStruts.LOCAL_ADDR_BYTES, (byte) 1);
//		session.write(sendCmd);//本地参数查询
    }

    /**
     * 当信息已经传送给客户端后触发此方法.
     */
    @Override
    public void messageSent(final IoSession arg0, final Object arg1) throws Exception {

        MainActivity.mainHandler.post(new Runnable() {
            @Override
            public void run() {
                if (arg1 instanceof IoBuffer) {
                    IoBuffer buffer = (IoBuffer) arg1;
                    buffer.flip();
                    String dataStr = buffer.getHexDump();

                    Msg msg1 = new Msg(dataStr, Msg.TYPE_SEND, Msg.sdf.format(new Date()));
                    WifiSetActivity.msgList.add(msg1);
                    WifiSetActivity.refreshWifiSetList();
                }

            }
        });
    }

    /**
     * 当连接被关闭时触发
     */
    @Override
    public void sessionClosed(IoSession arg0) throws Exception {
        // TODO Auto-generated method stub

    }

    /**
     * 当一个新客户端连接后触发此方法.
     */
    @Override
    public void sessionCreated(IoSession arg0) throws Exception {
        // TODO Auto-generated method stub

    }

    /**
     * 当连接空闲时触发此方法.
     */
    @Override
    public void sessionIdle(IoSession arg0, IdleStatus arg1) throws Exception {
        // TODO Auto-generated method stub

    }

    /**
     * 当接口中其他方法抛出异常未被捕获时触发此方法
     */
    @Override
    public void exceptionCaught(IoSession arg0, Throwable arg1)
            throws Exception {
    }

}
